package truedano.truedanostock;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ContinueAddStock extends Util {
	private static int LIST_CNT=5;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.continue_add_stock);
		setView();
	}
	
	private void setView(){
		LinearLayout[] addL = new LinearLayout[LIST_CNT];
		TextView[] stock = new TextView[LIST_CNT];
		final EditText[] number = new EditText[LIST_CNT];
		LinearLayout l1 = null;
		Button back = null,add_stock = null;
		
		LinearLayout continue_add_view = (LinearLayout)findViewById(R.id.continue_add_view);
		l1 = new LinearLayout(continue_add_view.getContext());
		continue_add_view.addView(l1);
		back = new Button(l1.getContext());
		back.setText(R.string.back);
		back.setBackgroundColor(Color.BLUE);
		back.setTextColor(Color.WHITE);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		l1.addView(back);
		
		add_stock = new Button(l1.getContext());
		add_stock.setText(R.string.add_stock);
		add_stock.setBackgroundColor(Color.BLUE);
		add_stock.setTextColor(Color.WHITE);
		add_stock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Thread getData = new Thread(){
					@Override
					public void run() {
						for(int i=0;i<LIST_CNT;i++){
							final String numTmp = number[i].getText().toString();
							if(numTmp.equals("") || numTmp.length()<4){
								continue;
							}
							if(isStockNumExist(numTmp) == true){
								continue;
							}
							String result = getHtmlResult(numTmp);
							String current_price = getCurrentPrice(result,numTmp);
							if(current_price.equals("")){
								setHandler(H_STOCK_NUMBER_ERROR,handler);
							}
							else{
								String stock_name = getStockName(result,numTmp);
								float div  = getCashDividend(numTmp);
								addDB(MyDB.STOCK_NUMBER,numTmp);
								writeAllDBItems(numTmp,current_price,stock_name,div);
							}
						}
						setHandler(H_ADD_SUCCESS,handler);
					}
				};
				getData.start();
				//close input keyboard//
				for(int i=0;i<LIST_CNT;i++){
					closeKeyboard(number[i]);
				}
			}
		});
		l1.addView(add_stock);
		
		for(int i=0;i<LIST_CNT;i++){
			addL[i]  = new LinearLayout(continue_add_view.getContext());
			continue_add_view.addView(addL[i]);
			
			stock[i] = new TextView(addL[i].getContext());
			stock[i].setText(getString(R.string.stock)+(i+1));
			addL[i].addView( stock[i] );
			
			number[i] = new EditText(addL[i].getContext());
			number[i].setEms(10);
			addL[i].addView( number[i] );
		}
	}
	
	public static final int H_ADD_SUCCESS     = 0;
	public static final int H_STOCK_NUMBER_ERROR = 1;
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what){
			case H_ADD_SUCCESS:
				showToast(getString(R.string.add_stock));
				break;
			case H_STOCK_NUMBER_ERROR:
				showToast(getString(R.string.stock_number_err));
				break;
			}
		}
	};
}
